/**
 * 2017年9月23日
 */
package exp.util;

import java.util.Arrays;
import java.util.Random;

import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
import org.opencv.core.Mat;

import com.alibaba.simpleimage.analyze.sift.ImagePixelArray;

/**
 * @author Alex
 *
 */
public class MatrixUtil {
	static Random rand = new Random();
	public static int[] getRandomArrayWithLength(int length){
		int []index = new int[length];
		for(int i = 0;i<index.length;i++)index[i]=i;
		int l = length ;
		for(int i = l;i>0;i--){
			int s = rand.nextInt(l);
			swap(index,s,i-1);
		}
		return index;
	}

	public static void main(String args[]){
		System.out.println(Arrays.toString(getRandomArrayWithLength(100)));
	}
	
	public static void swap(int index[],int l,int r){
		int temp = index[l];
		index[l] = index[r];
		index[r] = temp;
	}
	
	
	public static double [] add(double[] d1,double[]d2){
		for(int i=0;i<d1.length;i++){
			d1[i]+=d2[i];
		}
		return d1;
	}
	public static void test1(){
		float [][] a = new float[][]{{1,2,3,4,},{2,34,4}};
		System.out.println(Arrays.toString(flatFloatArray(a)));
		float [][][] b = new float[][][]{{{1,2,3},{4,5,6}},{{2,3,56},{2,8,9,0}}};
		System.out.println(Arrays.toString(flatFloatArray(b)));
	}
	public static float[]	flatFloatArray(float[][] arr){
		int num = 0;
		for(int i=0;i<arr.length;i++){
			num+=arr[i].length;
		}
		float res[] = new float[num];
		for(int i=0,acc=0;i<arr.length;acc+=arr[i].length,i++)
			System.arraycopy(arr[i],0, res, acc, arr[i].length);
		return res;
	}
	public static float[] flatFloatArray(float[][][] arr){
		int num = 0;
		for(int i=0;i<arr.length;i++){
			for(int j=0;j<arr[i].length;j++)
				num+=arr[i][j].length;
		}
		float [] res = new float[num];
		for(int i=0,acc=0;i<arr.length;i++){
			for(int j=0;j<arr.length;j++){
				System.arraycopy(arr[i][j], 0, res, acc, arr[i][j].length);
				acc+=arr[i][j].length;
			}
		}
		return res;
	}
	
	public static INDArray transform(INDArray from ,double low,double high){
		INDArray max = Nd4j.max(from);
		INDArray min = Nd4j.min(from);
		INDArray oR = max.sub(min);
		double rDiff = high - low;
		INDArray a = Nd4j.create(new double[]{rDiff});
		INDArray ratio = a.divi(oR);
		return from.subi(min).muli(ratio);
		
	}
	public static void printMat(Mat m){
		int width = (int)m.size().width;
		int height = (int)m.size().height;
		for(int i =0 ;i<width;i++){
			for(int j = 0;j<height;j++){
				System.out.print(Arrays.toString(m.get(j, i)));
				System.out.print(" ");
			}
			System.out.println();
		}
	}
	public static void fillMatRowByRow(Mat m,double[]img){
		for(int i =0;i<m.rows();i++){
			for (int j = 0;j<m.cols();j++){
				m.put(i, j, img[i*m.cols()+j]);
			}
		}
		
	}
	/**
	 * 对img移位填充
	 * @param m
	 * @param img
	 */
	public static void fillMatRowByRowShift(Mat m,double[]img){
		fillMatRowByRowShiftX(m, img, 1);
	}
	public static void fillMatRowByRowShiftX(Mat m,double[]img,int x){
		int beginIdx=0;
		int cols = m.cols();
		for(int i =0;i<m.rows();i++){
			for (int j = 0;j<cols;j++){
				m.put(i, j, img[(j+beginIdx)%cols]);
			}
			beginIdx += x ;
		}
	}
	public static ImagePixelArray fillImagePixelArrayRowByRowShiftX(double arr[]){
	
		int length = arr.length - 1;
		ImagePixelArray ipa = new ImagePixelArray(length,length);
		int beginIdx=0;
		for(int i = 0;i<length;i++){
			for(int j = 0;j<length;j++){
				ipa.data[i*length+j] = (float)arr[(j+beginIdx)%length];
			}
			beginIdx ++ ;
		}
		return ipa;
	}
	public static float[] doubleArrayToFloatArray(double []arr){
		float[]res = new float[arr.length];
		for(int i=0;i<arr.length;i++){
			res[i] = (float)arr[i];
		}
		return res;
	}
	public static double[] floatArrayToDoubleArray(float []arr){
		double[]res = new double[arr.length];
		for(int i=0;i<arr.length;i++){
			res[i] = arr[i];
		}
		return res;
	}
	
}
